package it.win4buy.services
{
    import flash.events.IEventDispatcher;

    import it.win4buy.events.GetStartuPlaceHolderListEvent;
    import it.win4buy.events.UtilityEvent;
    import it.win4buy.mock.RemoteObjectMock;
    import it.win4buy.utility.ApplicationConstants;
    import it.win4buy.utility.LoggerUtilty;

    import mx.collections.ArrayCollection;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.RemoteObject;

    /**
     *
     *	autore:
     *  pierangelo.mancusi@gmail.com
     *
     */

    public class GetStartupPlaceHolderService
    {



        //-------------------------------------------
        //
        // Injected properties
        //
        //-------------------------------------------

        [Inject]
        public var log:LoggerUtilty;

        [Inject]
        public var _ro:RemoteObjectMock;

        public var ro:RemoteObject;


        [Dispatcher]
        public var dispatcher:IEventDispatcher;

        //-------------------------------------------
        //
        // Private Properties
        //
        //-------------------------------------------



        //-------------------------------------------
        //
        // Getters / Setters
        //
        //-------------------------------------------


        //-------------------------------------------
        //
        // Constructor
        //
        //-------------------------------------------

        public function GetStartupPlaceHolderService()
        {
        }

        public function execute():void
        {
            log.logToConsole(this, "retriving PlaceHolder's List..");

            ro=new RemoteObject();
            ro.destination=ApplicationConstants.DESTINATION;
            ro.addEventListener(ResultEvent.RESULT, serviceResult);
            ro.addEventListener(FaultEvent.FAULT, serviceFault);

            ro.getTablesData();

        }


        //-------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------


        private function serviceResult(event:ResultEvent):void
        {
            log.logToConsole(this, "retriving PlaceHolder's List..OK");
            dispatcher.dispatchEvent(new GetStartuPlaceHolderListEvent(GetStartuPlaceHolderListEvent.RESULT, event.result as ArrayCollection, true));
            var temp:ArrayCollection=event.result as ArrayCollection;
            ro.removeEventListener(ResultEvent.RESULT, serviceResult);
            ro.removeEventListener(FaultEvent.FAULT, serviceFault);
            ro=null;
        }

        private function serviceFault(event:FaultEvent):void
        {
            log.logToConsole(this, "retriving PlaceHolder's List..KO" + "\n" + event.message.toString());
            dispatcher.dispatchEvent(new UtilityEvent(UtilityEvent.FAULT, event.message, true));

            ro.removeEventListener(ResultEvent.RESULT, serviceResult);
            ro.removeEventListener(FaultEvent.FAULT, serviceFault);
            ro=null;
        }



    }
}